package com.example.mybiadushoujiweishi.activity.acivitys;

import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;


import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.base.BaseActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.Contact;
import com.example.mybiadushoujiweishi.activity.acivitys.fragments.SelectContactFragment;
import com.example.mybiadushoujiweishi.activity.acivitys.fragments.ShowallContactsFragment;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class OnekeySerchActivity extends BaseActivity {
    ShowallContactsFragment showallContactsFragment = null;
    SelectContactFragment selectcontactfragment = null;
    @BindView(R.id.et_search)
    EditText etSearch;
    @BindView(R.id.input_contatas)
    Toolbar inputContatas;
    boolean flag = true;
    List<Contact> list;
    List<Contact> anyselectlist;
    @BindView(R.id.chosecontact)
    TextView chosecontact;
    @BindView(R.id.chosecontactnumber)
    TextView chosecontactnumber;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_onekey_serch);
        setSupportActionBar(inputContatas);
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        initview();
    }

    @Override
    public int getContentView() {
        return R.layout.activity_onekey_serch;
    }

    /**
     *
     */
    @Override
    public void initData() {

   //  int num=MyApplication.getSoslist().size()+AddContactsAdapter.getListsos().size();
     chosecontactnumber.setText("("+ MyApplication.getSoslist().size()+")");

        list = MyApplication.getList();
        anyselectlist = new ArrayList<Contact>();

        if (showallContactsFragment == null) {
            showallContactsFragment = new ShowallContactsFragment();
//            getFragmentManager().beginTransaction().replace(R.id.serch_framelayout,showallContactsFragment).addToBackStack("catac").commit();
        }

        getSupportFragmentManager().beginTransaction().
                replace(R.id.serch_framelayout, showallContactsFragment)
                .addToBackStack("contact")
                .commit();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            finish();
        }

    }




    public void initview() {

        etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
         //       Toast.makeText(OnekeySerchActivity.this, "======", Toast.LENGTH_SHORT).show();
                if (flag) {
                    if (selectcontactfragment == null) {
                        selectcontactfragment = new SelectContactFragment();
                    }

                    if (!selectcontactfragment.isAdded()) {
                        getSupportFragmentManager().beginTransaction().hide(showallContactsFragment)
                                .add(R.id.serch_framelayout, selectcontactfragment)
                                .addToBackStack("contact")
                                .commit();
                    }
                    flag = false;

                }

            }

            @Override
            public void onTextChanged(final CharSequence s, int start, int before, int count) {
                final String c = s.toString().replace(" ", "").replace("-", "");
                if (anyselectlist != null) {
                    anyselectlist.clear();
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        for (int i = 0; i < list.size(); i++) {
                            Contact contact = list.get(i);
                         //   System.out.println(contact.getData() + "}}}}}}}}}}}}}}}}}");
                            if (contact.getData().contains(c) || contact.getPhonenum().toString().replace(" ", "").replace("-", "").contains(c)) {
                                anyselectlist.add(contact);
                            }

                        }

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                selectcontactfragment.getSelectContact(anyselectlist);
                            }
                        });

                    }
                }).start();

//                selectcontactfragment.getSelectContact(anyselectlist);

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (etSearch.getText().length() == 0) {


                    if (getFragmentManager().getBackStackEntryCount() > 1) {
                        getFragmentManager().popBackStack();
                    }

                    flag = true;

                }

            }
        });


    }





}
